package com.sls.game.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.sls.game.dao.SupplyRuleDAO;
import com.sls.game.page.Pagination;
import com.sls.game.pojo.SupplyRuleInfo;
import com.sls.game.querybean.SupplyRuleQuery;

/**
 * 
 * @description 游戏供货规则表DAO
 * @author Wangxuegang
 * @date 2012-10-12
 * 
 */
@Repository("supplyRuleDAO")
public class SupplyRuleDAOImpl extends SuperDAO<SupplyRuleInfo> implements
		SupplyRuleDAO {

	/**
	 * 根据商品编号，单条记录查询
	 * 
	 * @param goodsId
	 * @return
	 */
	@Override
	public SupplyRuleInfo queryRecordBygoodsId(String goodsId) {
		SupplyRuleInfo info = null;
		info = this.getSqlSession().selectOne(
				"mybatis.xml.SupplyRuleMapper.queryRecordBygoodsId", goodsId);
		return info;
	}

	@Override
	public void insert(SupplyRuleInfo supplyRuleInfo) {
		this.getSqlSession().insert("mybatis.xml.SupplyRuleMapper.insertRule",
				supplyRuleInfo);
	}

	@Override
	public void update(SupplyRuleInfo supplyRuleInfo) {
		this.getSqlSession().update("mybatis.xml.SupplyRuleMapper.updateRule",
				supplyRuleInfo);
	}

	@Override
	public Pagination<SupplyRuleInfo> querySupplyRule(
			SupplyRuleQuery supplyRuleQuery) {
		Integer rows = this.queryRowCount(supplyRuleQuery);
		supplyRuleQuery.calculate();
		List<SupplyRuleInfo> list = this.queryPriceSystemList(supplyRuleQuery);
		Pagination<SupplyRuleInfo> page = this.queryPagination(rows, list,
				supplyRuleQuery);
		return page;
	}

	/**
	 * 查询总记录数
	 * 
	 * @param specialPriceSystemQuery
	 * @return
	 */
	private Integer queryRowCount(SupplyRuleQuery supplyRuleQuery) {
		Integer rows = this.getSqlSession().selectOne(
				"mybatis.xml.SupplyRuleMapper.querySupplyRuleRow",
				supplyRuleQuery);
		return rows;
	}

	/**
	 * 查询结果记录集
	 * 
	 * @param specialPriceSystemQuery
	 * @return
	 */
	private List<SupplyRuleInfo> queryPriceSystemList(
			SupplyRuleQuery supplyRuleQuery) {
		List<SupplyRuleInfo> list = this.getSqlSession().selectList(
				"mybatis.xml.SupplyRuleMapper.querySupplyRuleList",
				supplyRuleQuery);
		return list;
	}

	/**
	 * 根据id，单条记录查询
	 * 
	 * @param goodsId
	 * @return
	 */
	@Override
	public SupplyRuleInfo queryRecordById(String id) {
		SupplyRuleInfo info = null;
		info = this.getSqlSession().selectOne(
				"mybatis.xml.SupplyRuleMapper.queryRecordById", id);
		return info;
	}
}
